package cn.zfs.datasource.entity.model;


import org.springframework.util.StringUtils;

@SuppressWarnings("Duplicates")
public class SQLModelAndProperties {

    public static final int COLUMN_INDEX = 0;
    private static StringBuffer stringBuffer = new StringBuffer();

    public static String getTableNamesSql(String tableSchema) {
        stringBuffer.setLength(0);
        stringBuffer.append("select DISTINCT table_name as tableName from information_schema.`COLUMNS` where TABLE_SCHEMA=");
        stringBuffer.append("'");
        stringBuffer.append(tableSchema);
        stringBuffer.append("'");
        return stringBuffer.toString();
    }

    public static String getTableNamesSqlToLimit(String tableSchema, int pageSize, int pageNum, String tableName) {
        stringBuffer.setLength(0);
        stringBuffer.append("select DISTINCT table_name as tableName from information_schema.`COLUMNS` where TABLE_SCHEMA=");
        stringBuffer.append("'");
        stringBuffer.append(tableSchema);
        stringBuffer.append("'");
        if(!StringUtils.isEmpty(tableName)){
            stringBuffer.append(" ").append("and table_name like '%").append(tableName).append("%'");
        }
        stringBuffer.append(" limit ");
        stringBuffer.append(pageNum);
        stringBuffer.append(",");
        stringBuffer.append(pageSize);
        System.out.println(stringBuffer.toString());
        return stringBuffer.toString();
    }

    public static String getCount(String tableSchema) {
        stringBuffer.setLength(0);
        stringBuffer.append("select count(1) from (select DISTINCT table_name as tableName from information_schema.`COLUMNS` where TABLE_SCHEMA=");
        stringBuffer.append("'");
        stringBuffer.append(tableSchema);
        stringBuffer.append("'");
        stringBuffer.append(") as a");
        return stringBuffer.toString();
    }

    public static String getTablePropertiesSql(String tbaleName,String tableSchema) {
        stringBuffer.setLength(0);
        stringBuffer.append("select table_schema as tableSchema,table_name as tableName,column_name as columnName,column_comment as columnComment,is_nullable as isNullable,data_type as dataType from information_schema.columns where table_name=");
        stringBuffer.append("'");
        stringBuffer.append(tbaleName);
        stringBuffer.append("'");
        stringBuffer.append(" and ");
        stringBuffer.append(" table_schema='");
        stringBuffer.append(tableSchema);
        stringBuffer.append("'");
        return stringBuffer.toString();
    }

    public static String getDataTypes(String tbaleName){
        stringBuffer.setLength(0);
        stringBuffer.append("select DISTINCT data_type as dataType from information_schema.columns where table_name=");
        stringBuffer.append("'");
        stringBuffer.append(tbaleName);
        stringBuffer.append("'");
        return stringBuffer.toString();
    }

    public static String getColumnAndType(String tbaleName){
        stringBuffer.setLength(0);
        stringBuffer.append("select column_name as columnName,data_type as dataType from information_schema.columns where table_name=");
        stringBuffer.append("'");
        stringBuffer.append(tbaleName);
        stringBuffer.append("'");
        return stringBuffer.toString();
    }

    public static String getColumnNames(String tbaleName){
        stringBuffer.setLength(0);
        stringBuffer.append("select column_name from information_schema.columns where table_name=");
        stringBuffer.append("'");
        stringBuffer.append(tbaleName);
        stringBuffer.append("'");
        return stringBuffer.toString();
    }
}
